<template>
  <el-dialog
    :title="!dataForm.sinatvId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="sinatvStarttime">
      <el-input v-model="dataForm.sinatvStarttime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="sinatvEndtime">
      <el-input v-model="dataForm.sinatvEndtime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="直播标题" prop="sinatvTitle">
      <el-input v-model="dataForm.sinatvTitle" placeholder="直播标题"></el-input>
    </el-form-item>
    <el-form-item label="医生id" prop="docterId">
      <el-input v-model="dataForm.docterId" placeholder="医生id"></el-input>
    </el-form-item>
    <el-form-item label="直播封面" prop="sinatvImg">
      <el-input v-model="dataForm.sinatvImg" placeholder="直播封面"></el-input>
    </el-form-item>
    <el-form-item label="直播状态" prop="sinatvStatus">
      <el-input v-model="dataForm.sinatvStatus" placeholder="直播状态"></el-input>
    </el-form-item>
    <el-form-item label="直播数量" prop="sinatvNum">
      <el-input v-model="dataForm.sinatvNum" placeholder="直播数量"></el-input>
    </el-form-item>
    <el-form-item label="观看人数" prop="sinatvWatch">
      <el-input v-model="dataForm.sinatvWatch" placeholder="观看人数"></el-input>
    </el-form-item>
    <el-form-item label="收藏人数" prop="sinatvCollect">
      <el-input v-model="dataForm.sinatvCollect" placeholder="收藏人数"></el-input>
    </el-form-item>
    <el-form-item label="点赞人数" prop="sinatvLike">
      <el-input v-model="dataForm.sinatvLike" placeholder="点赞人数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sinatvId: 0,
          userId: '',
          sinatvStarttime: '',
          sinatvEndtime: '',
          sinatvTitle: '',
          docterId: '',
          sinatvImg: '',
          sinatvStatus: '',
          sinatvNum: '',
          sinatvWatch: '',
          sinatvCollect: '',
          sinatvLike: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          sinatvStarttime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          sinatvEndtime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          sinatvTitle: [
            { required: true, message: '直播标题不能为空', trigger: 'blur' }
          ],
          docterId: [
            { required: true, message: '医生id不能为空', trigger: 'blur' }
          ],
          sinatvImg: [
            { required: true, message: '直播封面不能为空', trigger: 'blur' }
          ],
          sinatvStatus: [
            { required: true, message: '直播状态不能为空', trigger: 'blur' }
          ],
          sinatvNum: [
            { required: true, message: '直播数量不能为空', trigger: 'blur' }
          ],
          sinatvWatch: [
            { required: true, message: '观看人数不能为空', trigger: 'blur' }
          ],
          sinatvCollect: [
            { required: true, message: '收藏人数不能为空', trigger: 'blur' }
          ],
          sinatvLike: [
            { required: true, message: '点赞人数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sinatvId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sinatvId) {
            this.$http({
              url: this.$http.adornUrl(`/live_streaming/sinatv/info/${this.dataForm.sinatvId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.sinatv.userId
                this.dataForm.sinatvStarttime = data.sinatv.sinatvStarttime
                this.dataForm.sinatvEndtime = data.sinatv.sinatvEndtime
                this.dataForm.sinatvTitle = data.sinatv.sinatvTitle
                this.dataForm.docterId = data.sinatv.docterId
                this.dataForm.sinatvImg = data.sinatv.sinatvImg
                this.dataForm.sinatvStatus = data.sinatv.sinatvStatus
                this.dataForm.sinatvNum = data.sinatv.sinatvNum
                this.dataForm.sinatvWatch = data.sinatv.sinatvWatch
                this.dataForm.sinatvCollect = data.sinatv.sinatvCollect
                this.dataForm.sinatvLike = data.sinatv.sinatvLike
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live_streaming/sinatv/${!this.dataForm.sinatvId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sinatvId': this.dataForm.sinatvId || undefined,
                'userId': this.dataForm.userId,
                'sinatvStarttime': this.dataForm.sinatvStarttime,
                'sinatvEndtime': this.dataForm.sinatvEndtime,
                'sinatvTitle': this.dataForm.sinatvTitle,
                'docterId': this.dataForm.docterId,
                'sinatvImg': this.dataForm.sinatvImg,
                'sinatvStatus': this.dataForm.sinatvStatus,
                'sinatvNum': this.dataForm.sinatvNum,
                'sinatvWatch': this.dataForm.sinatvWatch,
                'sinatvCollect': this.dataForm.sinatvCollect,
                'sinatvLike': this.dataForm.sinatvLike
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
